﻿using HalconDotNet;
using System;

namespace CCSSolderUI.Models
{
    public class Camera
    {
        #region 实例化
        private static Camera _instance;
        private static object _lock = new object();
        public static Camera Instance
        {
            get
            {
                if (Camera._instance == null)
                {
                    object @lock = Camera._lock;
                    lock (@lock)
                    {
                        if (Camera._instance == null)
                        {
                            Camera._instance = new Camera();
                        }
                    }
                }
                return Camera._instance;
            }
        }
        #endregion

        public event MessagePrintHandler MessagePrintEvent;
        private HFramegrabber Framegrabber { set; get; }
        HTuple AcqHandle;
        public bool OpenCamera(string cameraName, string CameraInterface = "GigEVision")
        {
            try
            {
                
                HOperatorSet.OpenFramegrabber(CameraInterface, 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", cameraName, 0, -1, out AcqHandle);
                Framegrabber = new HFramegrabber(AcqHandle);
                HOperatorSet.SetFramegrabberParam(AcqHandle, "GevSCPSPacketSize", 1200);
                HOperatorSet.SetFramegrabberParam(AcqHandle, "GevSCPD", 1000);
                return true;
            }
            catch (HalconException ex)
            {
                if (MessagePrintEvent != null)
                {
                    MessagePrintEvent(this, ex.Message);
                }
                return false;
            }
        }
        public void CloseCamera()
        {
            try
            {
                Framegrabber?.Dispose();
            }
            catch { }
        }
        public HImage GrabImage()
        {
            try
            {
                return Framegrabber.GrabImage();
            }
            catch (Exception ex)
            {
                if (MessagePrintEvent != null)
                {
                    MessagePrintEvent(this, ex.Message);
                }
                return null;
            }
        }
        public void SetExposureTime(HTuple exposure)
        {
            try
            {
                HOperatorSet.SetFramegrabberParam(AcqHandle, "ExposureTimeAbs", exposure);
            }
            catch { }
        }
        public HImage GrabEnhancedImage()
        {
            try
            {
                return new HImage(EnhancedImage(Framegrabber.GrabImage()));
            }
            catch (Exception ex)
            {
                if (MessagePrintEvent != null)
                {
                    MessagePrintEvent(this, ex.Message);
                }
                return null;
            }
        }
        public static HObject EnhancedImage(HObject image)
        {
            HObject imageScaled;
            HTuple mult = 255.0 / (255 - 70);
            HTuple add = -1 * mult * 70;
            HOperatorSet.ScaleImage(image, out imageScaled, mult, add);
            return imageScaled;
        }
        public void GrabeImageStart()
        {
            Framegrabber.GrabImageStart(-1);
        }
        public HImage GrabeImageAsync()
        {
            try
            {
                return Framegrabber.GrabImageAsync(-1);
            }
            catch (Exception ex)
            {
                if (MessagePrintEvent != null)
                {
                    MessagePrintEvent(this, ex.Message);
                }
                return null;
            }
        }
    }
}
